package edu.chl.jadetalk.business;

import edu.chl.jadetalk.db.IUserFacade;
import javax.ejb.Stateless;
import edu.chl.jadetalk.core.User;
import java.util.List;
import javax.ejb.EJB;

/**
 * Handles logic involed with user operations.
 * @author Daniel
 */
@Stateless
public class UserManagerBean implements UserManagerBeanLocal {

    @EJB
    private IUserFacade userFacade;
    
    @Override
    public User createUser(String userName, String password, String email) {
        User user = new User();
        user.setUsername(userName);
        user.setPassword(password);
        user.setEmail(email);
        userFacade.create(user);
        return user;
    }

    @Override
    public User loginUser(String userName, String password) {
        User user = null;
        user = userFacade.findByName(userName);

        if (user != null && password.equals(user.getPassword())) {
            return user;
        } else {
            return null;
        }

    }

    @Override
    public List<User> listUsers() {
        return userFacade.findAll();
    }

    @Override
    public void removeUser(User u) {
        userFacade.remove(u);
    }
}
